package com.hskn.hss.module.storageCenter.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.storageCenter.entity.TlStoragecenter;
import com.hskn.hss.module.storageCenter.mapper.TlStoragecenterMapper;
import com.hskn.hss.module.storageCenter.service.ITlStoragecenterService;
import com.hskn.hss.module.userdepart.entity.SysUserDepart;
import com.hskn.hss.module.userdepart.mapper.SysUserDepartMapper;
import com.hskn.hss.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-07
 */
@Service
public class TlStoragecenterServiceImpl extends ServiceImpl<TlStoragecenterMapper, TlStoragecenter> implements ITlStoragecenterService {

    @Resource
    TlStoragecenterMapper tlStoragecenterMapper;
    @Resource
    SysUserDepartMapper sysUserDepartMapper;

    @Override
    public AjaxResult insert(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        TlStoragecenter tlStoragecenter = params.getObject("data", TlStoragecenter.class);
        QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserDepart::getUserid, uid);
        String deptid = sysUserDepartMapper.selectOne(queryWrapper).getDepid();

        tlStoragecenter.setId(StringUtils.uuid());
        tlStoragecenter.setCreateby(uid);
        tlStoragecenter.setCreatetime(new Date());
        tlStoragecenter.setDeptid(deptid);
        int i = tlStoragecenterMapper.insert(tlStoragecenter);
        boolean flag = i > 0 ? true : false;

        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }


    @Override
    public AjaxResult update(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        TlStoragecenter tlStoragecenter = params.getObject("data", TlStoragecenter.class);
        tlStoragecenter.setUpdateby(uid);
        tlStoragecenter.setUpdatetime(new Date());
        int i = tlStoragecenterMapper.updateById(tlStoragecenter);

        boolean flag = i > 0 ? true : false;

        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }


    @Override
    public AjaxResult deleteByid(String id) throws HssException {
        try {
            tlStoragecenterMapper.deleteById(id);
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public TlStoragecenter getDetailById(String id) throws HssException {
        QueryWrapper<TlStoragecenter> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TlStoragecenter::getId, id);
        TlStoragecenter tlStoragecenter = tlStoragecenterMapper.selectOne(queryWrapper);
        return tlStoragecenter;
    }

    @Override
    public JSONObject getList(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        JSONObject json = new JSONObject();

        Map map = new HashMap();
        map.put("id", params.getString("id"));

        Page<TlStoragecenter> storagePage = new Page<>(page, size, true);
        IPage<TlStoragecenter> iPage = tlStoragecenterMapper.listStorage(storagePage, map);
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("btnlist", iPage.getRecords());
        return json;
    }
}
